package it.tcm.TeacherClassMate.servlet;

import it.tcm.TeacherClassMate.beans.OrderMainBean;
import it.tcm.TeacherClassMate.facade.OrderMainManageBase;
import it.tcm.TeacherClassMate.persistent.DBConnection;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.log4j.Logger;

public class DownloadZip extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(this.getClass());

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
		  Blob docx = null;
		  Blob result = null;
		  Connection conn = null;
		  Statement stmt0 = null;
		  Statement stmt1 = null;
		  ResultSet rs0 = null;
		  ResultSet rs1 = null;
		  try{
				
			  String id = request.getParameter("id");
			  OrderMainManageBase ommb = new OrderMainManageBase();
			  OrderMainBean omb = ommb.getOrderMainById(Integer.valueOf(id));
			  Long time = new Long(new Date().getTime());
			  String disposition = "attachment; fileName=" + "verifica" + time.toString() + ".zip";
			  String filename0 = omb.getUserId() + "-" + time.toString() + "_verifica.docx";
			  DBConnection db = new DBConnection();
			  conn = db.getDBConnection();	
			  
			  stmt0 = conn.createStatement();
			  rs0 = stmt0.executeQuery("select DOCX from ORDERMAIN where id = " + id);
			  InputStream in0 = null;
			  if (rs0.next()) {
				  docx = rs0.getBlob(1);
				  in0 = docx.getBinaryStream();
			  }
			  
			  String filename1 = omb.getUserId() + "-" + time.toString() + "_risultato.docx";
			  stmt1 = conn.createStatement();
			  rs1 = stmt1.executeQuery("select RESULT from ORDERMAIN where id = " + id);
			  InputStream in1 = null;
			  if (rs1.next()) {
				  result = rs1.getBlob(1);
				  in1 = result.getBinaryStream();
			  }
			
			  response.setContentType("application/zip");
	          response.setHeader("Content-Disposition", disposition);
			  ZipOutputStream outZip = new ZipOutputStream( response.getOutputStream() );
			  byte[] data0 = new byte[1000]; 
			  BufferedInputStream buin0 = new BufferedInputStream(in0);
			  int count;
			  outZip.putNextEntry(new ZipEntry(filename0));
			  while((count = buin0.read(data0,0,1000)) != -1) {  
				  outZip.write(data0, 0, count);
			  }
			  
			  byte[] data1 = new byte[1000]; 
			  BufferedInputStream buin1 = new BufferedInputStream(in1);
			  outZip.putNextEntry(new ZipEntry(filename1));
			  while((count = buin1.read(data1,0,1000)) != -1) {  
				  outZip.write(data1, 0, count);
			  }
			

			  in0.close();
			  in1.close();
			  buin0.close();
			  buin1.close();
			  logger.info("Your files are zipped");
			  outZip.flush();
			  outZip.close();
			  omb.setDownloaded(1);
			  ommb.save(omb);
			 		 		 
		  } catch (Throwable e) {
				 System.out.println("catch exception " +  e.getMessage()); 
				  e.printStackTrace();
				  return;
		  } finally {
				  try {
				  rs0.close();
				  stmt0.close();
				  conn.close();
				  } catch (SQLException e) {
				  e.printStackTrace();
			  }
		  }
			 
	}


	  protected void doPost(HttpServletRequest request, HttpServletResponse response)
	      throws ServletException, IOException 
	  {
		  
	  }      
	  
	  public String getServletInfo()
	  {
	    return super.getServletInfo();
	  }
	
	
	

}

